package com.zeng.sim.config;

import com.zeng.sim.controller.vo.SysUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-12-21T17:50
 */
public class LoginInterceptor implements HandlerInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        SysUserVo sysUserVo = (SysUserVo) request.getSession().getAttribute("currentUser");
        if (sysUserVo == null || !StringUtils.hasLength(sysUserVo.getUsername())) {
            LOGGER.info("请求URI[{}]无权限，重定向到登录页", request.getRequestURI());
            response.sendRedirect("/");
            return false;
        }
        return true;
    }

}
